import { Module } from 'vuex'
import { RootState } from '../types'
import request from '@/utils/request'

const productModule: Module<any, RootState> = {
  namespaced: true,

  state: {
    products: [],
    categories: []
  },

  mutations: {
    SET_PRODUCTS(state, products) {
      state.products = products
    },
    SET_CATEGORIES(state, categories) {
      state.categories = categories
    }
  },

  actions: {
    async getProducts({ commit }, params) {
      const response = await request.get('/products', { params })
      return response.data
    },

    async getCategories({ commit }) {
      const response = await request.get('/categories')
      commit('SET_CATEGORIES', response.data)
      return response.data
    }
  }
}

export default productModule 